package com.bh.vehicle.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.annotation.DataDictClass;
import com.bh.common.constant.Entity;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.SecurityUtils;
import com.bh.common.utils.ValidUtils;
import com.bh.vehicle.domain.JcDriver;
import com.bh.vehicle.mapper.JcDriverMapper;
import com.bh.vehicle.service.IJcDriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 司机管理Service业务层处理
 *
 * @author slx
 * @date 2023-04-24
 */
@Service
public class JcDriverServiceImpl extends ServiceImpl<JcDriverMapper, JcDriver> implements IJcDriverService {
    @Autowired
    private JcDriverMapper jcDriverMapper;

    /**
     * 查询司机管理
     *
     * @param id 司机管理主键
     * @return 司机管理
     */
    @DataDictClass
    @Override
    public JcDriver selectJcDriverById(Long id) {
        return jcDriverMapper.selectById(id);
    }

    @DataDictClass
    @Override
    public JcDriver selectJcDriverByUserId(String userId) {
        return jcDriverMapper.selectJcDriverByUserId(userId);
    }

    /**
     * 查询司机管理列表
     *
     * @param jcDriver 司机管理
     * @return 司机管理
     */
    @DataDictClass
    @Override
    public List<JcDriver> selectJcDriverList(JcDriver jcDriver) {
        return jcDriverMapper.selectJcDriverList(jcDriver);
    }

    /**
     * 新增司机管理
     *
     * @param jcDriver 司机管理
     */
    @Override
    public int saveOrUpdateJcDriver(JcDriver jcDriver) {
        int iAge = 0;
        String card = jcDriver.getCard();//获取身份证;
        if (card != null) {
            //健壮性判定, 身份证 验证 18位
            boolean idCard = ValidUtils.isIDCard(card);
            if (idCard) {// 如果身份证正确 验证成功 返回位TRUE
                String year1 = card.substring(6, 10);//获取身份证年
                String month1 = card.substring(10, 12);//获取身份证月
                String day1 = card.substring(12, 14);//获取身份证月
                String birthday3 = year1 + "-" + month1 + "-" + day1;

                Date birthday1 = null;
                try {
                    birthday1 = DateUtils.parseTimestamp(birthday3, "yyyy-MM-dd");
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                jcDriver.setBirthday(birthday1);
                //计算年纪
                Calendar cal = Calendar.getInstance();
                String year = card.substring(6, 10);
                int iCurrYear = cal.get(Calendar.YEAR);
                iAge = iCurrYear - Integer.valueOf(year);
                jcDriver.setAge(iAge);
            }
        }

        // 默认ID不会为空，id默认为 user_id
        JcDriver oldJcDriver = jcDriverMapper.selectById(jcDriver.getId());
        if (oldJcDriver != null) {
            jcDriver.setModifyName(SecurityUtils.getUsername());
            jcDriver.setModifyTime(DateUtils.getNowDate());
            jcDriver.setId(oldJcDriver.getId());
            return jcDriverMapper.updateById(jcDriver);
        }
        jcDriver.setJcOrganizationId(SecurityUtils.getOrgId());
        jcDriver.setCreateName(SecurityUtils.getUsername());
        jcDriver.setCreateTime(DateUtils.getNowDate());
        jcDriver.setStatus(Entity.ACTIVE);
        return jcDriverMapper.insert(jcDriver);
    }

    /**
     * 批量删除司机管理
     *
     * @param ids 需要删除的司机管理主键
     * @return 结果
     */
    @Override
    public int deleteJcDriverByIds(Long[] ids) {
        return jcDriverMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除司机管理信息
     *
     * @param id 司机管理主键
     * @return 结果
     */
    @Override
    public int deleteJcDriverById(Long id) {
        return jcDriverMapper.deleteById(id);
    }
}
